package com.juzipi.common.utils.json;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 封装json格式的类
 * 就是把处理异常时返回的json格式的数据做一个统一的封装更方便
 */

public class JsonAuthentication {

    protected void WriteJson(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,Object data) throws IOException {
        //中文乱码
        httpServletResponse.setContentType("text/json;charset=utf-8");
        //跨域
        httpServletResponse.setHeader("Access-Control-Allow-Origin","*");
        httpServletResponse.setHeader("Access-Control-Allow-method","POST,GET");
        //输出Json
        PrintWriter writer = httpServletResponse.getWriter();
        writer.write(JSON.toJSONString(data));
        //刷新一下
        writer.flush();
        //关闭
        writer.close();
    }

}
